//
//  PackageDetailCouponCell.m
//  BaseProject
//
//  Created by 王洋 on 2020/8/2.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "StoreDetailCouponReceiveCell.h"

#import "PackageDetailCouponCell.h"

@interface PackageDetailCouponCell()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>{
    CGSize _cellSize;
    UIEdgeInsets _collectionEdge;
}



@end

@implementation PackageDetailCouponCell

+ (CGFloat)calculateHeight {
    return 80;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self layout];
}

- (void)layout {
    
//    CGFloat width = TTScreenWidth - 80;
//
//    CGFloat cellWidth;
//    CGFloat distance = 5;
//    cellWidth = width/2 - 2*distance;
    
    CGFloat distance = 5;
    CGFloat height = 80;
    CGFloat width = 80 * 2.3;

    _cellSize =  CGSizeMake(width, height);
    _collectionEdge = UIEdgeInsetsMake(distance, distance, distance, distance);
    
    UICollectionViewFlowLayout *flowLaout = [[UICollectionViewFlowLayout alloc]init];
    flowLaout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

    self.collectionview.collectionViewLayout = flowLaout;
    self.collectionview.delegate = self;
    self.collectionview.dataSource = self;
    self.collectionview.showsHorizontalScrollIndicator = NO;
    self.collectionview.showsVerticalScrollIndicator = NO;
    
    [self.collectionview registerNib:[UINib nibWithNibName:@"StoreDetailCouponReceiveCell" bundle:nil] forCellWithReuseIdentifier:@"StoreDetailCouponReceiveCellID"];
    
}

- (void)fillCellWitArray:(NSMutableArray<CommonCouponModel *>*)array {
    self.dataArray = @[].mutableCopy;
    self.dataArray = array;
    
    self.couponCountLab.text = [NSString stringWithFormat:@"共%ld张券",array.count];
    
    [self.collectionview reloadData];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    StoreDetailCouponReceiveCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"StoreDetailCouponReceiveCellID" forIndexPath:indexPath];
    [cell fillCellWitModel:self.dataArray[indexPath.row]];

    return cell;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return _cellSize;
}

//设置cell的位置
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView
                       layout:(UICollectionViewLayout *)collectionViewLayout
       insetForSectionAtIndex:(NSInteger)section {

    return _collectionEdge;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.PackageDetailCouponCellBlock) {
        self.PackageDetailCouponCellBlock(indexPath.row);
    }
}

@end
